#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>

void handler(int signum){
	printf("%s\n",strsignal(signum));
}

void (*orig_handler)(int);
int main(){
	// SIGUSR1 SIGUSR2 默认动作是终止进程
	if((orig_handler=signal(SIGUSR1,handler))==SIG_ERR){
		perror("signal");
		exit(1);
	}
	pid_t pid_A=fork();
	if(pid_A==-1){
		perror("fork A");
		exit(1);
	}
	if(pid_A==0){
		pause();
		printf("A OK\n");
		exit(0);
	}else{
		pid_t pid_B=fork();
		if(pid_B==-1){
			perror("fork B");
			exit(1);
		}
		if(pid_B==0){
			pause();
			printf("B OK\n");
			exit(0);
		}
		sleep(1);
		kill(pid_A,SIGUSR1);
		kill(pid_B,SIGUSR2);
		waitpid(pid_A,NULL,0);
		waitpid(pid_B,NULL,0);
		printf("Main OK\n");
	}
	signal(SIGUSR1,orig_handler);
	return 0;
}

